/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * @author 品讯科技
 */

package com.px.pa.modulars.circle.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.px.basic.alone.core.base.BaseApiController;
import com.pig4cloud.pig.common.core.util.R;
import com.px.basic.alone.security.util.SecurityUtils;
import com.px.pa.modulars.circle.entity.CircleNotice;
import com.px.pa.modulars.circle.service.CircleNoticeService;
import com.px.pa.modulars.upms.entity.SysUser;
import com.px.pa.modulars.upms.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 公告通知
 *
 * @author 品讯科技
 * @date 2024-08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/circlenotice" )
@Api(value = "circlenotice", tags = "公告通知管理")
public class CircleNoticeController  {

    private final  CircleNoticeService circleNoticeService;

    @Autowired
    private SysUserService sysUserService;
    private SysUser getUser(){

        System.out.println(SecurityUtils.getUser());
        SysUser user = sysUserService.getById(SecurityUtils.getUser().getId());
        return user;
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param circleNotice 公告通知
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getCircleNoticePage(Page page, CircleNotice circleNotice) {
        return R.ok(circleNoticeService.page(page, new QueryWrapper<CircleNotice>()
        .eq("tenant_code",getUser().getOnlyLogo())));
    }


    /**
     * 通过id查询公告通知
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(circleNoticeService.getById(id));
    }

    /**
     * 新增公告通知
     * @param circleNotice 公告通知
     * @return R
     */
    @ApiOperation(value = "新增公告通知", notes = "新增公告通知")
    @PostMapping
    public R save(@RequestBody CircleNotice circleNotice) {

        circleNotice.setCreateBy(getUser().getUserId());
        circleNotice.setTenantCode(getUser().getOnlyLogo());
        circleNotice.setCreateTime(LocalDateTime.now());

        return R.ok(circleNoticeService.save(circleNotice));
    }

    /**
     * 修改公告通知
     * @param circleNotice 公告通知
     * @return R
     */
    @ApiOperation(value = "修改公告通知", notes = "修改公告通知")
    @PutMapping
    public R updateById(@RequestBody CircleNotice circleNotice) {
        return R.ok(circleNoticeService.updateById(circleNotice));
    }

    /**
     * 通过id删除公告通知
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除公告通知", notes = "通过id删除公告通知")
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(circleNoticeService.removeById(id));
    }

}
